﻿//作品展示幻灯片切换代码

function initCarousel () {
    if ($(".jcarousel-container").length) {
        $(".jcarousel-container").remove();
        $("#carousel_holder").append("<div id='mycarousel'><ul></ul></div>");
        $(".picture").html("");
    }
    jQuery('#mycarousel').jcarousel({
        scroll: 5,
        wrap: "last",
        itemFallbackDimension: 300,
        itemLoadCallback: mycarousel_itemLoadCallback
    });
}

function mycarousel_itemLoadCallback(carousel, state)
{
    if (state != 'init')
        return;

    var category = $(".nav .current a").attr("category");
    $.ajax({
        url: "./data/GetPictureByCategory.aspx?category=" + category,
        dataType: "json",
        success: function (data) {
            mycarousel_itemAddCallback(carousel, carousel.first, carousel.last, data);
        }
    });
};

function mycarousel_itemAddCallback(carousel, first, last, data)
{
    var items = data;

    for (i = 0; i < items.length; i++) {
        if (i == 0) {
            showPic(items[i].ImageName);
            showDescription();
        }
        carousel.add(i+1, mycarousel_getItemHTML(items[i]));
    }

    carousel.size(items.length);
};

function showPic (imageName) {
    if ($(".picture img").length) {
        $(".picture img").fadeOut(function () {
            $(".picture").html("").append("<img style='display:none;' src='./pictures/" + imageName + "' />");
            $(".picture img").fadeIn();
        });
    } else {
        $(".picture").append("<img style='display:none;' src='./pictures/" + imageName + "' />");
        $(".picture img").fadeIn();
    }
};

function mycarousel_getItemHTML(imgObj)
{
    var imageNameString = "'" + imgObj.ImageName + "'";
    return '<a href="#"><img src="./pictures/' + imgObj.smallImageName + '" onclick="showPic(' + imageNameString + ')" /></a>';
};

//作品展示幻灯片切换代码结束

function showDescription () {
    
    var category = $(".nav .current a").attr("category");
    $.ajax({
        url: "./data/GetCategoryDescriptionByName.aspx?category=" + category,
        dataType: "json",
        success: function (data) {
            $("#description p").html(data.CategoryDescription);
            $("#description").show();
        }
    });
}

//作品类别切换代码

function setupNav () {
    $("#body_wrap .nav a").click (function (ev) {
        if (!$(ev.target).hasClass("current")) {
            $("#body_wrap .nav li").removeClass("current");
            $(ev.target).parent().addClass("current");
            
            //update pictures
            initCarousel();
            
            //update description
            showDescription();
        }
    });
};

//作品类别切换代码结束

$(document).ready(function () {
    initCarousel();
    setupNav();
});